<?php

namespace app\admin\controller\datacenter;

use app\common\controller\Backend;

use think\Controller;
use think\Request;
use think\Db;
use app\admin\model\VOnlineinfo;
use function GuzzleHttp\json_decode;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Onlineinfot extends Backend
{
    /**
     * VOnlineinfo模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('VOnlineinfo');

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
     * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        //按时间搜索
        $startime = (int)$this->request->param('ids');
        if($startime<=0){
            $this->error('参数丢失');
        }
        
        $map['createtime'] = $startime;
        
        //搜索30条
        $t_num = [];
        $info = $this->model->where($map)->find();
        $list = json_decode($info['online'],true);//转数据
        
        foreach ($list as $key=>$val){
            $time = date('H:00',(float)sprintf('%.0f',(floatval($val['time']))/1000));
            if(isset($t_num[$time]) && $val['num']<$t_num[$time]){
                continue;
            }
            $t_num[date('H:00',(float)sprintf('%.0f',(floatval($val['time']))/1000))] = $val['num'];//在线人数
        }
        
        $this->view->assign([
            't_num'          => $t_num,
            'is_mobile' => is_mobile()?:0,
            'endtime' => date('Y-m-d',$startime/1000),
            'startime' => date('Y-m-d',$startime/1000),
        ]);
        
        return $this->view->fetch();
    }
    
    /**
     * 每天订单统计列表
     */
    public function lists(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        //如果发送的来源是Selectpage，则转发到Selectpage
        if ($this->request->request('pkey_name'))
        {
            return $this->selectpage();
        }
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        
        //搜索条件
        $filter = $this->request->get("filter", '');
        $filter_arr = json_decode($filter,true);
        $map['createtime'] = $this->getMilliDate($filter_arr['createtime'].' 0:0:0');
        
        $list = $this->model->where($map)->find();
        
        $arr = [];
        $lists = json_decode($list['online'],true);//转数据
        $total = count($lists);
        $lists = array_slice($lists,$offset,$limit);
        
        foreach ($lists as $key=>$val){
            $arr[$key]['createtime'] = $val['time'];
            $arr[$key]['online'] = $val['num'];
        }
        $list = $this->changedata($list,$arr);

        //统计
        $result = array("total" => $total, "rows" => $list, "statistics"=>[]);
    
        return json($result);
    }

}
